package com.github.postsanf.yinian.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;

public class LookSendWebActivity extends AppCompatActivity {
    private WebView look_send_webView;
    private ProgressDialog mProgressDialog;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && look_send_webView.canGoBack()) {
            look_send_webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_send_web);
        initView();
    }

    private void initView() {
        String ebOrderLogisticsCompany = getIntent().getStringExtra("ebOrderLogisticsCompany");
        String ebOrderLogisticsNumber = getIntent().getStringExtra("ebOrderLogisticsNumber");
        look_send_webView = ((WebView) findViewById(R.id.look_send_webView));
        look_send_webView.setDrawingCacheEnabled(true);
        look_send_webView.getSettings().setDefaultTextEncodingName("utf-8");
        look_send_webView.getSettings().setJavaScriptEnabled(true);
        look_send_webView.addJavascriptInterface(new WebAppInterface(), "Android");
        String tarUrl = "https://m.kuaidi100.com/index_all.html?type=" + ebOrderLogisticsCompany + "&postid=" + ebOrderLogisticsNumber;
        look_send_webView.loadUrl(tarUrl);
        look_send_webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });

        mProgressDialog = ProgressDialog.show(this, "", CommonConstants.TIP_LOAD_DATA);
        look_send_webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    mProgressDialog.dismiss();
                } else {

                    // 加载中
                    mProgressDialog.show();
                }
            }
        });
    }


    public class WebAppInterface {
        @JavascriptInterface
        public void doFinish() {
            finish();
        }
    }
}
